**************************************************************
**RISK AND DEMAND FOR SOCIAL PROTECTION IN AN ERA OF POPULISM*
**CODE TO REPLICATE FIGURES AND TABLES IN MAIN MANUSCRIPT*****
**Last Edit: July 20, 2022, by KB*****************************
**************************************************************

log using "Covidrisk_manuscript_replication.log", replace

global esttabformat b(%8.2f) se(%8.2f) obs r2(%8.2f) star(+ 0.10 * 0.05 ** 0.01)

use "data/Covidrisk_wave1.dta", replace


****************************************************
*FIGURE 2 statistics (graph created using R script)*
****************************************************

*summarize constructed Covid risk variable (statistics reported in text)
sum knowCovid

*statistics reported in figure 2
sum selfCovid_dummy familyCovid_dummy friendCovid_dummy coworkerCovid_dummy noCovid_dummy

****************************************************
*FIGURE 3 statistics (graph created using R script)*
****************************************************

*summarize constructed employment risk variable (statistics reported in text)
sum jobeffectCovid3

*statistics reported in figure 3
sum selfrestoredhrs_dummy selfreducedhrs_dummy selfrestoredlayoff_dummy selftemplayoff_dummy selflayoff_dummy familyrestoredhrs_dummy familyreducedhrs_dummy familyrestoredlayoff_dummy familytemplayoff_dummy familylayoff_dummy noeconshock_dummy

*********************************
*FIGURE 4: SUBJECTIVE INSECURITY*
*********************************

use "data/Covidrisk_wave2.dta", replace

*Calculate differences between treatments*
*tech vs. anti-foreign
ttest risk if treatment>0 & treatment<3, by(treatment)
*anti-foreign versus anti-elite
ttest risk if treatment>1, by(treatment)
*tech versus anti-elite
ttest risk if treatment!=0 & treatment!=2, by(treatment)

set scheme plotplain

preserve

collapse (mean) meanrisk= risk (sd) sdrisk=risk (count) n=risk, by(treatment)

generate hirisk = meanrisk + invttail(n-1,0.025)*(sdrisk / sqrt(n))
generate lowrisk = meanrisk - invttail(n-1,0.025)*(sdrisk / sqrt(n))

*Enhanced Bar Graph*
graph twoway (bar meanrisk treatment if treatment==0) (bar meanrisk treatment if treatment==1) ///
 (bar meanrisk treatment if treatment==2) (bar meanrisk treatment if treatment==3) (rcap hirisk lowrisk treatment) /// 
 (scatteri 2.9 1 2.9 2,  recast(line) lw(medthick)  mc(none) lc(black) lp(solid)) ///
 (scatteri 2.9 1 2.9 2,  recast(dropline) base(2.88) lw(medthick) mc(none) lc(black) lp(solid)) ///
 (scatteri 2.93 2 2.93 3,  recast(line) lw(medthick)  mc(none) lc(black) lp(solid)) ///
 (scatteri 2.93 2 2.93 3,  recast(dropline) base(2.91) lw(medthick) mc(none) lc(black) lp(solid)),  ///
  text(2.91 1.5 "{&Delta} = 0.088 (SE=0.053)" , size(small)) text(2.94 2.5 "{&Delta} = 0.101 (SE=0.052)" , size(small)) ytitle("Average Subjective Insecurity") xlabel(0 "Control" 1 "Tech" 2 "Anti-Foreign" 3 "Anti-Elite", noticks) xtitle("") legend( order(1 "Control" 2 "Technocratic" 3 "Anti-Foreign" 4 "Anti-Elite") )

graph export "figures_tables/Figure4.tif", replace
	   
restore

***********************************************************************
*FIGURE 5: AVERAGE SUPPORT FOR REDISTRIBUTION BY HEALTH SHOCK EXPOSURE*
***********************************************************************

use "data/Covidrisk_wave1.dta", replace

*Tabulation of health shock categorical variable (statistics reported in text)
tab healthshock

*Calculate differences between treatments for those with highest risk exposure*
*tech vs. anti-foreign
ttest pandemicsupport if treatment>0 & treatment<3 & healthshock==2, by(treatment)
*anti-foreign versus anti-elite
ttest pandemicsupport if treatment>1 & healthshock==2, by(treatment)
*tech vs. anti-elite
ttest pandemicsupport if treatment!=0 & treatment!=2 & healthshock==2, by(treatment)

preserve

collapse (mean) meansupport= pandemicsupport (sd) sdsupport=pandemicsupport (count) n=pandemicsupport, by(healthshock treatment)

generate hisupport = meansupport + invttail(n-1,0.025)*(sdsupport / sqrt(n))
generate lowsupport = meansupport - invttail(n-1,0.025)*(sdsupport / sqrt(n))

gen healthtreatment = treatment+1 if healthshock==0
replace healthtreatment = treatment+6 if healthshock==1
replace healthtreatment = treatment+11 if healthshock==2

twoway (bar meansupport healthtreatment if treatment==0) ///
       (bar meansupport healthtreatment if treatment==1) ///
       (bar meansupport healthtreatment if treatment==2) ///
       (bar meansupport healthtreatment if treatment==3) ///
       (rcap hisupport lowsupport healthtreatment), ///
	   yscale(range(1 4)) ylabel(1 (1) 4) ///
	   xlabel( 2.5 "Low" 7.5 "Middle" 12.5 "High", noticks) ///
	   xtitle("Health Shock") ytitle("Mean Support Redistribution") ///
       legend( order(1 "Control" 2 "Technocratic" 3 "Anti-Foreign" 4 "Anti-Elite") )
graph export "figures_tables/Figure5.tif", replace
	   
restore

***********************************************************************
*FIGURE 6: AVERAGE SUPPORT FOR REDISTRIBUTION BY ECONOMIC SHOCK EXPOSURE*
***********************************************************************

*Find natural break in economic shock amongst respondents exposed to a shock*
sort jobeffectCovid3
group1d jobeffectCovid3 if jobeffectCovid3>=0.01, max(2) gen(g2=2)

*Tabulation of employment shock categorical variable (statistics reported in text)
tab econshock

*Calculate differences between treatments for those with highest risk exposure*
*tech vs. anti-foreign
ttest pandemicsupport if treatment>0 & treatment<3 & econshock==2, by(treatment)
*anti-foreign versus anti-elite
ttest pandemicsupport if treatment>1 & econshock==2, by(treatment)
*tech vs. anti-elite
ttest pandemicsupport if treatment!=0 & treatment!=2 & econshock==2, by(treatment)

preserve

collapse (mean) meansupport= pandemicsupport (sd) sdsupport=pandemicsupport (count) n=pandemicsupport, by(econshock treatment)

generate hisupport = meansupport + invttail(n-1,0.025)*(sdsupport / sqrt(n))
generate lowsupport = meansupport - invttail(n-1,0.025)*(sdsupport / sqrt(n))

gen econtreatment = treatment+1 if econshock==0
replace econtreatment = treatment+6 if econshock==1
replace econtreatment = treatment+11 if econshock==2

twoway (bar meansupport econtreatment if treatment==0) ///
       (bar meansupport econtreatment if treatment==1) ///
       (bar meansupport econtreatment if treatment==2) ///
       (bar meansupport econtreatment if treatment==3) ///
       (rcap hisupport lowsupport econtreatment), ///
	   yscale(range(1 4)) ylabel(1 (1) 4) ///
	   xlabel( 2.5 "Low" 7.5 "Middle" 12.5 "High", noticks) ///
	   xtitle("Economic Shock") ytitle("Mean Support Redistribution") ///
       legend( order(1 "Control" 2 "Technocratic" 3 "Anti-Foreign" 4 "Anti-Elite") )
graph export "figures_tables/Figure6.tif", replace
	   
restore


*********** 
**TABLE 3**
*********** 
 
eststo m1a: reg pandemicsupport tech for elite knowCovid, robust
eststo m1b: reg pandemicsupport tech for elite techXknow forXknow eliteXknow knowCovid, robust
lincom (for + forXknow) - (elite + eliteXknow)
lincom (for + forXknow) - (tech + techXknow)
lincom (elite + eliteXknow) - (tech + techXknow)
eststo m1c: reg pandemicsupport tech for elite jobeffectCovid3, robust
eststo m1d: reg pandemicsupport tech for elite techXjobeffectCovid3 forXjobeffectCovid3 eliteXjobeffectCovid3 jobeffectCovid3, robust
lincom (for + forXjobeffectCovid3) - (elite + eliteXjobeffectCovid3)
lincom (for + forXjobeffectCovid3) - (tech + techXjobeffectCovid3)
lincom (elite + eliteXjobeffectCovid3) - (tech + techXjobeffectCovid3)

esttab m1a m1b m1c m1d using "figures_tables/Poptable3.rtf", order(for elite tech knowCovid forXknow eliteXknow techXknow jobeffectCovid3 forXjobeffectCovid3 eliteXjobeffectCovid3 techXjobeffectCovid3) $esttabformat replace label onecell
*Note: lincom estimates added manually to table*

******************************************************************************
*FIGURE 7*
*DIFFERENCE IN THE EFFECTS OF POPULIST MESSAGES BY HEALTH AND ECONOMIC SHOCKS*
******************************************************************************

set scheme tufte

reg pandemicsupport knowCovid for elite tech forXknow eliteXknow techXknow, robust

predictnl diff_1 = (_b[for] + _b[forXknow]*knowCovid) - (_b[elite] + _b[eliteXknow]*knowCovid), se(diff1_se)

gen upper1 = diff_1 + diff1_se*1.96
gen lower1 = diff_1 - diff1_se*1.96

twoway (line diff_1 upper1 lower1 knowCovid, sort lcolor(gs2) lpattern(solid dash dash)), yline(0, lcolor(gs2) lpattern(dot)) legend(position(5) ring(0)) xtitle("Exposure to Health Shock", color(gs2)) ///
       ytitle("Differential Effect of Anti-Foreign vs. Anti-Elite", color(gs2)) ylabel(-0.5 (0.5) 2,nogrid) yscale(range(-0.5 (0.5) 2)) ///
	   xlabel (0 (0.5) 1) graphregion(fcolor(white) ifcolor(white) color(white) lcolor(white) ilcolor(white)) ///
	   legend(order(1 "Effect" 2 "95 % CIs"))
graph export "figures_tables/Figure7_leftpanel.tif", replace

reg pandemicsupport jobeffectCovid3 for elite tech forXjobeffectCovid3 eliteXjobeffectCovid3 techXjobeffectCovid3, robust

predictnl diff_2 =  (_b[for] +_b[forXjobeffectCovid3]*jobeffectCovid3) - (_b[elite]+ _b[eliteXjobeffectCovid3]*jobeffectCovid3), se(diff2_se)

gen upper2 = diff_2 + diff2_se*1.96
gen lower2 = diff_2 - diff2_se*1.96

twoway (line diff_2 upper2 lower2 jobeffectCovid3, sort lcolor(gs2) lpattern(solid dash dash)), yline(0, lcolor(gs2) lpattern(dot)) legend(position(5) ring(0)) xtitle("Exposure to Economic Shock", color(gs2)) ///
       ytitle("Differential Effect of Anti-Foreign vs. Anti-Elite", color(gs2)) ylabel(-0.5 (0.5) 2,nogrid) yscale(range(-0.5 (0.5) 2)) ///
	   graphregion(fcolor(white) ifcolor(white) color(white) lcolor(white) ilcolor(white)) ///
	   legend(order(1 "Effect" 2 "95 % CIs"))

graph export "figures_tables/Figure7_rightpanel.tif", replace
	   
log close
